package test;


import org.junit.Test;

import sqlengine.Main;



public class Insert {
	
	static int score = 0;
	
	@Test
	public void easyInsert(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f1,f2) values ('abc', 'efg');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			out = m.process("insert into simple1(f2) values ('abc');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();	
					
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){
			score+= Breakdown.iEasy;
			SC.score.append("\n\t insertRecord works in very basic situations - " + Breakdown.iEasy + "/" + Breakdown.iEasy);
		}else{
			SC.score.append("\n\t insertRecord doesn't work even in very basic situations - 0/" + Breakdown.iEasy);
		}	
	}
	
	@Test 
	public void insertPrimayKey(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f1,f2) values ('abc', 'efg');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			out = m.process("insert into simple1(f1,f2) values ('abc', 'efg');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();	
					
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){
			score+= Breakdown.iKey;
			SC.score.append("\n\t insertRecord checks primary key constraint - " + Breakdown.iKey + "/" + Breakdown.iKey);
		}else{
			SC.score.append("\n\t insertRecord doesn't check primary key constraint - 0/" + Breakdown.iKey);
		}
	}
	
	@Test
	public void insert(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			StringBuilder input1 = new StringBuilder();
			StringBuilder input2 = new StringBuilder();
			input1.append("create table simple1 (");
			for(int i = 0; i < 100; i++){
				if(i != 30)
					input1.append("f" + i + " varchar2(30), ");
				else
					input1.append("f" + i + " varchar2(30) primary key, ");
			}
			input1.append("f100 varchar2(30));\n");
			
			input2.append("insert into simple1 (");
			for(int i = 0; i < 100; i++)
					input2.append("f" + i +", " );
			
			input2.append("f100) values (");
			for(int i = 0; i < 100; i++)
				input2.append("'AAA',");
			input2.append("'AAA');\n");
			
			Main m = new Main();
			out = m.process(input1.toString());
			out = m.process(input2.toString());
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f2,f1) values ('abc', 'efg');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f2,f1) values ('abcdefghijklm', 'efg');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f1,f2) values ('abcdefghijklm', 'efg');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f1) values ('abcdefghijklm', 'efg');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f2) values ('abcdefghijklm');\n");
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f2, f1, f1) values ('abcdefghijklm', 'abc', 'abc');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f2, f3) values ('abcdefghijklm', 'abc');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("insert into simple1(f1, f2) values ('abcdem');\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();	
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("INSERT into simple1(f1, f2) \nvAlUes ('abcdem', 'abc');\n");  //removed extra 'e' from values
			if(out == null || !out.equals("1 row created.\n"))
				throw new Exception();
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){
			score+= Breakdown.iFull;
			SC.score.append("\n\t insertRecord works properly in general situations - " + Breakdown.iFull + "/" + Breakdown.iFull);
		}else{
			SC.score.append("\n\t insertRecord doesn't work properly in general situations - 0/" + Breakdown.iFull);
		}	
	}
}
